<?php

use Illuminate\Support\Facades\Hash;

if (! function_exists('generateCode')) {
//    function generateCode($password)
//    {
//        $random_str = uniqid() . '|' . $password;
//        $urlime_str = substr(time(), 6, -1);
//        return strtoupper(substr(md5($random_str.$urlime_str), 0, 15));
//    }
    function generateCode($uid)
    {
        $random_str = (int) (100000 + $uid);
        return $random_str;
    }
}

if (! function_exists('generateKey')) {
    function generateKey($code)
    {
        return Hash::make($code);
    }
}

if (! function_exists('generateAppCode')) {
    /**
     * @param  $classify
     *
     * @return string
     *
     */
    function generateAppCode($classify)
    {
        $prefix = '';
        switch ($classify) {
            case 0:
                $prefix = 'WEB'; break;
            case 1:
                $prefix = 'AD'; break;
            case 2:
                $prefix = 'IOS'; break;
            default:break;
        }
        $salt = uniqid().time();
        $str = md5($salt);
        return $prefix.substr($str, 0, -strlen($prefix));
    }
}

/**
 * curl抓取网页方法
 *
 * @param string $url  目标网址
 * @param array  $data 传输数据
 * @param string $urlype 请求类型
 * @param int    $ssl  是否是https网关
 * @param bool   $raw  是否输出原生内容
 *
 * @param bool   $is_mobile 是否是手机ua
 *
 * @return array $output 返回内容  [body]页面内容 [http_code]是响应码
 */
function curlHttp($url, $data=[], $urlype='get', $ssl=1, $raw=false, $is_mobile = false)
{
    $output = [];
    $data = is_array($data) ? http_build_query($data) : $data;
    if ($urlype == 'get') {
        $url = $url . '?' . $data;
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    if ($urlype == 'post') {
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
    }
    if ($ssl) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,CURLOPT_BINARYTRANSFER,1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    curl_setopt($ch,CURLOPT_MAXREDIRS,8);
//    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_REFERER, true);
//    curl_setopt($ch, CURLOPT_COOKIEJAR, true);
//    curl_setopt($ch, CURLOPT_COOKIEFILE, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $ua = $is_mobile
        ? 'Mozilla/5.0 (iPad; U; CPU OS 3_2_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B500 Safari/531.21.10'
//        ? 'Dalvik/1.6.0 (Linux; U; Android 4.1.2; DROID RAZR HD Build/9.8.1Q-62_VQW_MR-2)'
        : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36';
    curl_setopt($ch,CURLOPT_USERAGENT, $ua);
    $res = curl_exec($ch);
    if ($raw) {
        return curl_getinfo($ch);
    }
    $res = convertToUTF8($res);
    $output['body'] = $res === false ? 'CURL RETURN ERROR:' . curl_error($ch) : $res;
    $output['http_code'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $output;
}

if (!function_exists('createNonceStr')) {
    function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}

if (!function_exists('hidePrivateData')) {
    function hidePrivateData($str, $bank = false) {
        if (strlen($str) <= 11) {
            $str = substr_replace($str, '****', 2, 4);
        } else {
            if ($bank)
                $str = substr_replace($str, '**** **** **** ', 0, 12);
            else
                $str = substr_replace($str, '*****', 4, strlen($str) - 8);
        }
        return $str;
    }
}

if (!function_exists('luhmBank')) {
    function luhmBank($bankNo) {
        $n = 0;
        for ($i = strlen($bankNo); $i >= 1; $i--) {
            $index=$i-1;
         //偶数位
            if ($i % 2==0) {
               $n += $bankNo{$index};
            } else {//奇数位
                $url = $bankNo{$index} * 2;
                if ($url > 9) {
                    $url = (int)($url/10)+ $url%10;
                }
                $n += $url;
            }
        }
     return ($n % 10) == 0;
    }
}

if (!function_exists('convertToUTF8')) {
    function convertToUTF8($words) {
        $charset = mb_detect_encoding($words, array('ASCII','GB2312','GBK','UTF-8'));
        if ($charset != "UTF-8") {
            $words = iconv($charset, "UTF-8//IGNORE", $words);
        }
        return $words;
    }
}

if (!function_exists('parseEmailWeb')) {
    function parseEmailUrl($email) {
        $url = explode('@', $email);
        if (count($url) != 2) {
            return null;
        } else{
            $url = strtolower($url[1]);
            if ($url == '163.com') {
                return 'https://mail.163.com';
            } else if ($url == 'vip.163.com') {
                return 'https://vip.163.com';
            } else if ($url == '126.com') {
                return 'https://mail.126.com';
            } else if ($url == 'qq.com' || $url == 'vip.qq.com' || $url == 'foxmail.com') {
                return 'https://mail.qq.com';
            } else if ($url == 'gmail.com') {
                return 'https://mail.google.com';
            } else if ($url == 'sohu.com') {
                return 'https://mail.sohu.com';
            } else if ($url == 'tom.com') {
                return 'https://mail.tom.com';
            } else if ($url == 'vip.sina.com') {
                return 'https://vip.sina.com';
            } else if ($url == 'sina.com.cn' || $url == 'sina.com') {
                return 'https://mail.sina.com.cn';
            } else if ($url == 'tom.com') {
                return 'https://mail.tom.com';
            } else if ($url == 'yahoo.com.cn' || $url == 'yahoo.cn') {
                return 'https://mail.cn.yahoo.com';
            } else if ($url == 'tom.com') {
                return 'https://mail.tom.com';
            } else if ($url == 'yeah.net') {
                return 'https://www.yeah.net';
            } else if ($url == '21cn.com') {
                return 'https://mail.21cn.com';
            } else if ($url == 'hotmail.com') {
                return 'https://www.hotmail.com';
            } else if ($url == 'sogou.com') {
                return 'https://mail.sogou.com';
            } else if ($url == '188.com') {
                return 'https://www.188.com';
            } else if ($url == '139.com') {
                return 'https://mail.10086.cn';
            } else if ($url == '189.cn') {
                return 'https://webmail15.189.cn/webmail';
            } else if ($url == 'wo.com.cn') {
                return 'https://mail.wo.com.cn/smsmail';
            } else if ($url == '139.com') {
                return 'https://mail.10086.cn';
            } else {
                return null;
            }

        }
    }
}
